#include "board.h"
#include<QPainter>
#include<QMouseEvent>
board::board(QWidget *parent) : QWidget(parent)
{
    for(int i=0;i<32;i++)//初始化棋子
    {
        _s[i].init(i);
    }
    _selectid=-1;//初始化点击
}
void board::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    int d=40;
    _r=d/2;
    for(int i=1;i<=10;i++)
    {
        painter.drawLine(QPoint(d,i*d),QPoint(9*d,i*d));
    }//画十条横线
    for(int i=1;i<=9;i++)
    {
        if(i==1||i==9)
        painter.drawLine(QPoint(i*d,d),QPoint(i*d,10*d));
        else
        {
           painter.drawLine(QPoint(i*d,d),QPoint(i*d,5*d));
           painter.drawLine(QPoint(i*d,6*d),QPoint(i*d,10*d));
        }
    }//画9条竖线
    painter.drawLine(QPoint(4*d,1*d),QPoint(6*d,3*d));
    painter.drawLine(QPoint(4*d,3*d),QPoint(6*d,1*d));
    painter.drawLine(QPoint(4*d,8*d),QPoint(6*d,10*d));
    painter.drawLine(QPoint(4*d,10*d),QPoint(6*d,8*d));
    //画九宫格
    for(int i=0;i<32;i++)
    {
        drawstone(painter,i);
    } //绘制32颗棋子
}

QPoint board::center(int row,int col)
{
    QPoint ret;
    ret.rx()=(col+1)*_r*2;
    ret.ry()=(row+1)*_r*2;
    return ret;
}
QPoint board::center(int id)
{
    return center(_s[id]._row,_s[id]._col);
}

void board::drawstone(QPainter&painter, int id)
{
    if(_s[id]._dead==true)
        return;

    QPoint c=center(id);
    QRect rect=QRect(c.x()-_r,c.y()-_r,_r*2,_r*2);

    //画背景
    if(id==_selectid)
    {
        painter.setBrush(QBrush(Qt::yellow));
    }
    else
    {
        painter.setBrush(QBrush(Qt::white));
    }

    //设置字的初始颜色
    painter.setPen(Qt::black);

    painter.drawEllipse(center(id),_r,_r);//调用drawEllipse（椭圆）函数
    if(_s[id]._red)//当该棋子的种类为红时设置棋子的字的颜色为红色
    {
        painter.setPen(Qt::red);
    }
    painter.setFont(QFont("system",_r,700));//修改字体的大小及样式
    painter.drawText(rect,_s[id].getText(),QTextOption(Qt::AlignCenter));//设置字的文本
}

bool board::getRowCol(QPoint pt, int &row, int &col)//设置获取点击的位置
{
    for(row=0;row<=9;row++)
    {
        for(col=0;col<=8;col++)
        {
            QPoint c=center(row,col);
            int dx=c.x()-pt.x();
            int dy=c.y()-pt.y();
            int dist=dx*dx+dy*dy;
            if(dist<_r*_r)
                return true;
        }//使用距离
    }
    return false;
}

void board::mouseReleaseEvent(QMouseEvent *ev)
{
    QPoint pt=ev->pos();
    //将pt转化为象棋的行列值
    //判断这个行列值上有无棋子
    int row,col;
    bool bRet=getRowCol(pt,row,col);
    if(bRet==false)
        return;//未点击到可识别的位置

    int i;
    int clickid=-1;//对点击的初始化
     for(i=0;i<32;i++)
     {
         if(_s[i]._row==row&&_s[i]._col==col&&_s[i]._dead==false)
         {
             break;
         }
     }
     if(i<32)
     {
         clickid=i;
     }

    if(_selectid==-1)
    {
        if(clickid!=-1)
        {
            _selectid=clickid;
            update();
        }
         if(i<32)
         {
           _selectid=i;
           update();
       }
    }
    else
    {
        _s[_selectid]._row=row;
        _s[_selectid]._col=col;
        if(clickid!=-1)
        {
            _s[clickid]._dead=true;
        }
        _selectid=-1;
        update();
    }
}
